package cn.com.xiaoshoujia.activities.service.impl;

import cn.com.xiaoshoujia.activities.activity.ActivitiesActivity;
import cn.com.xiaoshoujia.activities.activity.ActivitiesJoinActivity;
import cn.com.xiaoshoujia.activities.dto.*;
import cn.com.xiaoshoujia.activities.service.ActivitiesJoinService;
import cn.com.xiaoshoujia.application.service.SMSService;
import cn.com.xiaoshoujia.domain.enums.SignName;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dulei
 * @Title: ActivityJoinServiceImpl
 * @Description: ActivityJoinService实现类
 * @Time 2017/7/30 21:20
 */
@Service
public class ActivitiesJoinServiceImpl implements ActivitiesJoinService {

    Logger logger = LoggerFactory.getLogger(ActivitiesJoinServiceImpl.class);

    @Autowired
    ActivitiesJoinActivity activitiesJoinActivity;

    @Autowired
    ActivitiesActivity activitiesActivity;

    @Autowired
    SMSService smsService;

    /**
     * @Author: dulei
     * @Description: 查找团成员
     * @param: headId 团ID id 团购活动ID
     * @Date: 2017/7/30 21:36
     */
    public ActivityJoinResultDTO findTeamJoinResult(Long headId, Long id) throws UnsupportedEncodingException {
        return activitiesJoinActivity.findTeamJoin(headId, id);
    }

    /**
     * @Author: dulei
     * @Description:
     * @param: id 团购活动ID clueId 报名信息表单ID
     * @Date: 2017/7/30 21:58
     */
    @Override
    public boolean sendMsg(Long id, Long clueId) {
        logger.debug("获得用户手机号");
        String cluesPhone = activitiesJoinActivity.findCluesPhone(clueId);
        if (StringUtils.isNotBlank(cluesPhone)) {
            logger.debug("获得短信内容");
            ActivitySmsMsgDTO smsMsg = activitiesActivity.findCSmsMsg(id);
            sendMsg(cluesPhone, smsMsg);
            return true;
        }
        return false;
    }

    /**
     * @Author: dulei
     * @Description: 查看购买人详细信息
     * @param: clueId 购买人员表单ID
     * @Date: 2017/7/31 14:38
     */
    public ActivityCluesDetailDTO findDetail(Long clueId) throws UnsupportedEncodingException {
        return activitiesJoinActivity.findDetail(clueId);
    }

    /**
     * @Author: dulei
     * @Description: 向C端发送短信
     * @Date: 2017/7/30 22:16
     */
    public void sendMsg(String phone, ActivitySmsMsgDTO smsMsg) {
        Map<String, String> map = new HashMap<>();
        logger.debug("给map赋值");
        map.put("name", StringUtils.substring(smsMsg.getTitle(), 0, 10));
        map.put("address", smsMsg.getAddress());
        map.put("time", smsMsg.getStartTime());
        map.put("comname", smsMsg.getSponsor());
        map.put("contact", smsMsg.getPhone());
        logger.debug("发送短信");
        smsService.sendMessage(phone, SignName.SHAREBANG, "SMS_147437986", map);
    }

    @Override
    public boolean reSendMsg(Long id, Long clueId) {
        ActivityCluesMsgDTO activityCluesMsgDTO = activitiesJoinActivity.findUserMsg(clueId);
        if (null==activityCluesMsgDTO){
            logger.error("未查询到用户 clueId = {}",clueId);
            return false;
        }
        if (StringUtils.isBlank(activityCluesMsgDTO.getCluesPhone())){
            logger.error("用户手机号未空 clueid = {},activityCluesMsgDTO = {}",clueId,activityCluesMsgDTO);
            return false;
        }
        logger.debug("用户信息 msg = {}",activityCluesMsgDTO);
        ActivitySmsMsgDTO smsMsg = activitiesActivity.findCSmsMsg(id);
        if (activityCluesMsgDTO.getType().intValue()==1){
            sendSingleMsg(smsMsg,activityCluesMsgDTO.getCluesPhone());
        }else if (activityCluesMsgDTO.getType().intValue()==0){
            Integer status = activitiesActivity.findTeamStatusByHeadId(activityCluesMsgDTO.getTeamId());
            if (status.intValue()==-1){
                //参团失败
                sendFailTeamMsg(smsMsg,activityCluesMsgDTO.getCluesPhone());
            }else if (status.intValue()==1 || status.intValue()==2){
                //进行中
                String time = activitiesActivity.findTeamEndTime(activityCluesMsgDTO.getTeamId());
                sendTeamMsg(smsMsg,time,activityCluesMsgDTO.getCluesPhone());
            }else if (status.intValue()==3){
                //已成功
                sendSuccessTeamSms(smsMsg,activityCluesMsgDTO.getCluesPhone());
            }else {
                logger.error("无发送短信状态 status = {}",status);
                return false;
            }
        }else {
            logger.error("数据库保存类型不未1或0 type = {}",activityCluesMsgDTO.getType());
            return false;
        }
        return true;
    }

    /**
     * @Author: dulei
     * @Description: 团进行中 发送短信
     * @Date: 2017/8/25 15:32
     */
    public void sendTeamMsg(ActivitySmsMsgDTO smsMsg, String time, String phone) {
        Map<String, String> map = new HashMap<>();
        logger.debug("给map赋值");
        map.put("name", StringUtils.substring(smsMsg.getTitle(), 0, 10));
        map.put("time", time);
        map.put("comname", smsMsg.getSponsor());
        map.put("contact", smsMsg.getPhone());
        logger.debug("发送短信");
        smsService.sendMessage(phone, SignName.SHAREBANG, "SMS_147437977", map);
    }

    /**
     * @Author: dulei
     * @Description: 团成功 发送短信
     * @param:  * @param null
     * @Date: 2017/8/25 15:08
     */
    public void sendSuccessTeamSms(ActivitySmsMsgDTO smsMsg, String phone) {
        Map<String, String> map = new HashMap<>();
        logger.debug("给map赋值");
        map.put("name", StringUtils.substring(smsMsg.getTitle(), 0, 10));
        map.put("comname", smsMsg.getSponsor());
        map.put("contact", smsMsg.getPhone());
        logger.debug("发送短信");
        smsService.sendMessage(phone, SignName.SHAREBANG, "SMS_147437974", map);
    }

    /**
     * @Author: dulei
     * @Description: 团不成功 发送短信
     * @param:  * @param null
     * @Date: 2017/8/25 15:06
     */
    private void sendFailTeamMsg(ActivitySmsMsgDTO smsMsg, String phone) {
        Map<String, String> map = new HashMap<>();
        logger.debug("给map赋值");
        map.put("name", StringUtils.substring(smsMsg.getTitle(), 0, 10));
        map.put("comname", smsMsg.getSponsor());
        map.put("contact", smsMsg.getPhone());
        logger.debug("发送短信");
        smsService.sendMessage(phone, SignName.SHAREBANG, "SMS_147418226", map);
    }

    /**
     * @Author: dulei
     * @Description: 向单买用户发送短信
     * @Date: 2017/8/25 14:51
     */
    public void sendSingleMsg(ActivitySmsMsgDTO smsMsg,String phone) {
        Map<String, String> map = new HashMap<>();
        logger.debug("给map赋值");
        map.put("name", StringUtils.substring(smsMsg.getTitle(), 0, 10));
        map.put("comname", smsMsg.getSponsor());
        map.put("contact", smsMsg.getPhone());
        logger.debug("发送短信");
        smsService.sendMessage(phone, SignName.SHAREBANG, "SMS_147437981", map);
    }

    /*--------------------wx start--------------------*/

    /**
     * @Author: dulei
     * @Description: 查询用户的团信息
     * @Date: 2017/8/3 18:48
     */
    public ActivityUserTeamDTO findUserJoin(ActivityWxParamDTO wxParam) throws UnsupportedEncodingException,RuntimeException {

        return activitiesJoinActivity.findUserJoin(wxParam.getSysId(), wxParam.getId());
    }

    /**
     * @Author: dulei
     * @Description: 查询分享者团信息
     * @Date: 2017/8/3 18:48
     */
    public ActivityShareTeamDTO shareJoin(ActivityWxParamDTO activityWxParamDTO) throws UnsupportedEncodingException {
        return activitiesJoinActivity.shareJoin(activityWxParamDTO);
    }

    /**
     * @Author: dulei
     * @Description: 单买未支付，返回信息
     * @param: sysId 用户唯一编码 id 活动ID
     * @Date: 2017/8/5 15:39
     */
    public ActivityOrderJoinDTO teamNoPay(String sysId, Long id, Integer type) {
        return activitiesJoinActivity.teamNoPay(sysId, id, type);
    }

    @Override
    public ActivitiesCluesRemarkDTO findRemark(Long id) {
        return activitiesJoinActivity.findRemark(id);
    }

    @Override
    public boolean updateRemark(Long id, String remark) {
        return activitiesJoinActivity.updateRemark(id,remark);
    }

    @Override
    public boolean changeVolumeStatus(Long id, Integer status) {
        return activitiesJoinActivity.changeVolumeStatus(id,status);
    }
    /*--------------------wx end----------------------*/
}
